/*
 * $Id: AppEnv.cc,v 1.6 2006-02-06 14:58:14 bacon Exp $
 */

#include "Globals.h"
#include "AppEnv.h"
#include <xp/bas/stdlib.h>
#include <xp/bas/string.h>

AppEnv::AppEnv (xp_size_t mpb_size): SkvEnv (mpb_size)
{

}

void AppEnv::preLoad ()
{
	// TODO:  removeAllItems...

	addItem (APP_ENV_LOGGING_TARGET,
		APP_LOGGING_TARGET_FILE,
		(ProbeProc)&AppEnv::probe_logging_target);

	addItem (
		APP_ENV_LOGGING_LEVEL,
		xp_getlogpriorityname(XP_LOG_DEBUG),
		(ProbeProc)&AppEnv::probe_logging_level);

	addItem (
		APP_ENV_GATE_ENABLE_TCP,
		APP_YES,
		(ProbeProc)&AppEnv::probe_gate_enable_tcp);

	addItem (
		APP_ENV_GATE_TCP_SERVICE,
		APP_GATE_TCP_SERVICE,
		(ProbeProc)&AppEnv::probe_gate_tcp_service);	

	addItem (
		APP_ENV_GATE_TCP_LOCAL_ONLY,
		APP_YES,
		(ProbeProc)&AppEnv::probe_gate_tcp_local_only);	

	addItem (
		APP_ENV_GATE_MAX_CONNECTIONS,
		XP_QUOTE(APP_GATE_MAX_CONNECTIONS),
		(ProbeProc)&AppEnv::probe_gate_max_connections);

	addItem (
		APP_ENV_GATE_TIMEOUT,
		XP_QUOTE(APP_GATE_TIMEOUT),
		(ProbeProc)&AppEnv::probe_gate_timeout);
}

void AppEnv::postLoad ()
{
	// the new value is only effective when the program is restarted.
	//xp_strxcpy (masqmon_wan_orig, xp_countof(masqmon_wan_orig), masqmon_wan);
}

int AppEnv::probe_logging_target (const xp_char_t* v)
{
	int target = 0;
	const xp_char_t* p = v;
	xp_char_t* tok; 
	xp_size_t tok_len;

	while (p != XP_NULL) {
		p = xp_strtok (p, XP_TEXT(","), &tok, &tok_len);
		if (xp_strxcmp(tok, tok_len, APP_LOGGING_TARGET_FILE) == 0)
			target |= XP_LOG_FILE;
		else if (xp_strxcmp(tok, tok_len, APP_LOGGING_TARGET_SYSLOG) == 0)
			target |= XP_LOG_SYSLOG;
		else if (xp_strxcmp(tok, tok_len, APP_LOGGING_TARGET_CONSOLE) == 0)
			target |= XP_LOG_CONSOLE;
		else return -1;
	};

	xp_log_settarget (&app_log, target);
	return 0;
}

int AppEnv::probe_logging_level (const xp_char_t* v)
{
	if (xp_log_setprioritybyname(&app_log, v) == -1) return -1;
	return 0;
}

int AppEnv::probe_gate_enable_tcp (const xp_char_t* v)
{
	if (xp_strcmp (v, APP_YES) == 0) {
		gate_enable_tcp = true;
		return 0;
	}
	else if (xp_strcmp (v, APP_NO) == 0) {
		gate_enable_tcp = false;
		return 0;
	}

	return -1;
}

int AppEnv::probe_gate_tcp_service (const xp_char_t* v)
{
	xp_size_t len = xp_strlen(v);
	if (len <= 0 || len > TcpAddress::MAX_SERVICE_LEN) return -1;
	xp_strxcpy (gate_tcp_service, xp_countof(gate_tcp_service), v);
	return 0;
}

int AppEnv::probe_gate_tcp_local_only (const xp_char_t* v)
{
	if (xp_strcmp (v, APP_YES) == 0) {
		gate_tcp_local_only = true;
		return 0;
	}
	else if (xp_strcmp (v, APP_NO) == 0) {
		gate_tcp_local_only = false;
		return 0;
	}

	return -1;
}

int AppEnv::probe_gate_max_connections (const xp_char_t* v)
{
	if (v[0] == XP_CHAR('\0')) return -1;
	if (!xp_stristype(v,xp_isdigit)) return -1;

	/* don't care about the overflow */

	unsigned int num;
	XP_STRTOI (num, v, XP_NULL, 10);
	if (num < APP_GATE_MAX_CONNECTIONS_MIN ||
	    num > APP_GATE_MAX_CONNECTIONS_MAX) return -1;

	gate_max_connections = num;
	if (app_main != XP_NULL) {
		app_main->tcp_gate.setMaxConnections (gate_max_connections);
	}
	return 0;
}

int  AppEnv::probe_gate_timeout (const xp_char_t* v)
{
	if (v[0] == XP_CHAR('\0')) return -1;
	if (!xp_stristype (
		((v[0] == XP_CHAR('-'))? &v[1]: v), xp_isdigit)) return -1;

	/* don't care about the overflow */

	xp_time_t num;
	XP_STRTOI (num, v, XP_NULL, 10);
	if (num < APP_GATE_TIMEOUT_MIN ||
	    num > APP_GATE_TIMEOUT_MAX) return -1;

	gate_timeout = num;
	return 0;
}
